class kindListItem extends how.module.ItemView
{
	private gameName: how.Label;
	private onlineNums: how.Label;
	private kindID: number;
	private bg: eui.Image;
	private downloaderPanel: eui.Panel;
	private loadingTxt: eui.Label;
	private loadingImg: eui.Image;

	public constructor() 
	{
		super();
		this.skinName = "kindItem";
	}

	public start(): void 
	{
	}

	public dataChanged(): void 
	{
		this.kindID = this.data.kindID;
		this.gameName.text = this.data.kindID + "_" + this.data.gameName;

		if(egret.Capabilities.isMobile && egret.Capabilities.runtimeType != egret.RuntimeType.WXGAME)
		{
			this.downloaderPanel.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onGameDownloadClick, this);
			egret.ExternalInterface.addCallback("checkGameExits_" + this.kindID, this.checkGameExitsCallback.bind(this));
			egret.ExternalInterface.call("checkGameExits", this.kindID.toString());
		}
		else
		{
			this.onResourceCompleteNew(this.kindID.toString());
			this.downloaderPanel.visible = false;
		}
	}

	public checkGameExitsCallback(result):void
    {
		console.log("checkGameExitsCallback checkGameExitsCallback :" + result);
        let results: string[] = result.split(',');
        let kid = parseInt(results[0]);
		if(kid == this.kindID)
		{
			let isExits = results[1];
			if(isExits == "1")
			{
				this.bg.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onGameItemClick, this);
				this.downloaderPanel.visible = false;
			}
			else
			{
				this.downloaderPanel.visible = true;
			}
		}
	}

	public onGameItemClick(event: egret.TouchEvent): void
    {
        if(this.kindID)
        {
			this.report(MainSceneModule.request.onGameBtn, { id: this.kindID });
        }
        else
        {
            console.warn("Unknown KindID");
        }
    }

	public onGameDownloadClick(event: egret.TouchEvent): void
	{
		let loadResGroup: Array<string> = DataConfig.ResConfig[this.kindID];
		if(!loadResGroup) loadResGroup = [];
		how.Application.LoadResourceNew(this.kindID, loadResGroup, this.onResourceCompleteNew.bind(this));
		this.loadingTxt.text = "正在下载...";
		this.loadingImg.visible = true;
		egret.Tween.get(this.loadingImg, { loop: true }).to({rotation:360}, 1000);
	}

	public onResourceCompleteNew(data: any): void
    {
		if(data == this.kindID.toString())
		{
        	egret.ExternalInterface.call("downloadGameOK", data.toString());
			egret.Tween.removeAllTweens();
			this.bg.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onGameItemClick, this);
			this.loadingImg.visible = false;
			this.downloaderPanel.visible = false;
		}
    }
}

window["kindListItem"] || (window["kindListItem"] = kindListItem);
